/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.rw.table.formatter;

import java.text.DecimalFormat;
import java.text.ParseException;
import javax.swing.SwingConstants;

/**
 *
 * @author Rene Tapia
 */
public class DoubleCellFormatter implements ICellFormatter {

    private DecimalFormat df;

    public DoubleCellFormatter(int decimals) {
        this.df = (DecimalFormat) DecimalFormat.getInstance();
        df.setMaximumFractionDigits(decimals);
    }

    @Override
    public String format(Object value) {
        double dblValue = ((Double) value).doubleValue();
        if (Double.isNaN(dblValue)) {
            return "NaN";
        } else if (Double.isInfinite(dblValue)) {
            return dblValue >= 0 ? "+Inf" : "-Inf";
        } else {
            return df.format(dblValue);
        }
    }

    @Override
    public Object parse(String strValue) throws ParseException {
        return df.parse(strValue).doubleValue();
    }

    @Override
    public int getAlign() {
        return SwingConstants.RIGHT;
    }
}
